﻿using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System;
using EOS.Framework.Tools.Aspects;
using EOS.Framework.Rules;

#pragma warning disable 0618

namespace EOS.Framework.Web.Mvc.Controls
{
    public class Textarea<TValue> : InputControl<TValue>
    {
        private const string DefaultErrorMessageClass = "error_tooltip";
        private const string DefaultErrorIconClass = "error_icon";
        private const string DefaultErrorClass = "form_error";

        private string _errorIcon;

        [NotEmpty]
        public string ErrorIcon
        {
            get { return _errorIcon; }
            set { _errorIcon = value; }
        }

        private string _errorIconClass = DefaultErrorIconClass;

        [NotEmpty]
        public string ErrorIconClass
        {
            get { return _errorIconClass; }
            set { _errorIconClass = value; }
        }

        private string _errorClass = DefaultErrorClass;

        [NotEmpty]
        public string ErrorClass
        {
            get { return _errorClass; }
            set { _errorClass = value; }
        }

        private string _errorMessageClass = DefaultErrorMessageClass;

        [NotEmpty]
        public string ErrorMessageClass
        {
            get { return _errorMessageClass; }
            set { _errorMessageClass = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Textarea&lt;TValue&gt;"/> class.
        /// </summary>
        public Textarea(IForm form)
            : base(form)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="Textarea&lt;TValue&gt;"/> class with
        /// the name of the error icon and a set of rules.
        /// </summary>
        /// <param name="errorIcon">The path to the error icon. Cannot be null or empty or contains only strings.</param>
        /// <param name="rules">An array of rules.</param>
        [ParamNotEmpty("errorIcon")]
        public Textarea(IForm form, string errorIcon, params Rule[] rules)
            : base(form)
        {
            this._errorIcon = errorIcon;

            foreach (Rule rule in rules)
            {
                Rules.Add(rule);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Textarea&lt;TValue&gt;"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="name">The name.</param>
        [ParamNotNullOrEmpty("value"), ParamNotNullOrEmpty("name")]
        public Textarea(IForm form, string value, string name)
            : base(form, name)
        {
            InputValue = value;
        }

        protected override void OnRender(IXhtmlWriter writer, PageContext context)
        {
            if (TagName.ToLower(CultureInfo.CurrentCulture).Equals("textarea", StringComparison.CurrentCulture) == false)
            {
                throw new ControlMarkupException(
                    string.Format(CultureInfo.CurrentCulture, Properties.Resources.InvalidTagName, "input"));
            }

            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                WriteBeginAttachedTag(writer, new string[] { "text", "class" });

                string errorClass = GetAttributeValue("class");
                if (string.IsNullOrEmpty(errorClass))
                {
                    errorClass = this._errorClass;
                }
                else
                {
                    errorClass += " " + this._errorClass;
                }
                writer.WriteAttribute("class", errorClass);
            }
            else
            {
                WriteBeginAttachedTag(writer, new string[] { "text" });
            }


            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                string errorDivName = Name + "Error";

                writer.WriteAttribute("onFocus", string.Format(CultureInfo.CurrentCulture, "Show('{0}')", errorDivName));
                writer.WriteAttribute("onBlur", string.Format(CultureInfo.CurrentCulture, "Hide('{0}')", errorDivName));
            }
            writer.WriteString(InputValue);
            WriteEndAttachedTagFull(writer);

            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                string imagePath = ToAbsolutePath(_errorIcon);

                writer.WriteStartElement("img");
                writer.WriteAttribute("src", imagePath);
                writer.WriteAttribute("alt", "ErrorImage");
                writer.WriteAttribute("class", _errorIconClass);
                writer.WriteEndElement();
            }

            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                writer.WriteStartElement("div");

                writer.WriteAttribute("id", Name + "Error");
                if (!string.IsNullOrEmpty(_errorMessageClass))
                {
                    writer.WriteAttribute("class", _errorMessageClass);
                }
                writer.WriteString(ErrorMessage);

                writer.WriteEndElement();
            }
        }
    }
}
